package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 缘友一世
 * date 2023/7/21-17:37
 * 菜品管理
 */
@RestController
@Slf4j
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {

        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);
        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    @Transactional
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);

        HashSet<Long> set = new HashSet<>();
        for (Long id : ids) {
            Long categoryId = dishService.getCategoryIdById(id);
            set.add(categoryId);
        }

        //redis缓存的是以categray_id为键，该分类下的菜品数据为值的形式
        //删除，对应分类categray_id的的键值对
        for (Long categoryId : set) {
            cleanCache("dish_"+categoryId);
        }

        dishService.deleteBatch(ids);

        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);

        Long categoryId = dishDTO.getCategoryId();

        cleanCache("dish_"+categoryId);

        return Result.success();
    }

    /**
     * 修改菜品的起，停售状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result UpdateStatus(@RequestParam("id") Long id,@PathVariable("status") int status){
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishService.UpdateStatus(dish);

        Long categoryId = dishService.getCategoryIdById(id);
        //将相应的菜品缓存数据清理掉
        cleanCache("dish_"+categoryId);

        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id/菜品名称查询菜品")
    public Result<List<Dish>> list(@RequestParam(name ="categoryId",required = false) Long categoryId,@RequestParam(name = "name", required = false) String name) {
        List<Dish> lists= dishService.list(categoryId,name);
        return Result.success(lists);
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
